
import numpy as np
import matplotlib.pyplot as plt

###############################################

# read kinetics output
def filereader(fname):
    tempdat = {}
    kin = open(fname,'r')
    temp = kin.readline()
    while not 'EDDY' in temp:
        temp = kin.readline()
    temp = kin.readline()
    temp = kin.readline()
    temp = kin.readline().split()
    z, t, p, d, eddy = [],[],[],[],[]
    while len(temp)>1:
        z+= [float(temp[1])]
        t+=[float(temp[2])]
        d+= [float(temp[3])]
        p+=[float(temp[4])]
        eddy+=[float(temp[7])]
        temp = kin.readline().split()
    temp = kin.readline()
    while not 'MIX' in temp:
        temp = kin.readline()
    temp = kin.readline()
    header = kin.readline().split()
    while len(header)>1:
        print(header)
        for ix in header[1:]:
            tempdat[ix] = []
        temp = kin.readline().split()
        while len(temp)>1:
            for ix in range(1,len(header)):
                tempdat[header[ix]] += [float(temp[ix+1])]
            temp = kin.readline().split()
        header = kin.readline().split()
    tempdat['z'], tempdat['t'], tempdat['p'], tempdat['d'], tempdat['eddy'] = z,t,p,d,eddy
    return tempdat

############################################### 

# read input
dat = {}
infile = open('input.dat','r')
temp = infile.readline().split()
while len(temp) > 1:
    if not temp[1] in list(dat.keys()):
        dat[temp[1]] = {}
    dat[temp[1]][temp[3]] = filereader(temp[2])
    temp = infile.readline().split()

############################################### 

# create plot
fig,ax=plt.subplots(ncols=3,nrows=2,figsize=(8,6))
lns = [':','-.','--','-','o-','*']
colors = ['blue','green','purple','magenta','red','orange']
lw = 1.6
for i in list(dat.keys()):
    jctr = 0
    for j in list(dat[i].keys()):
        print(dat[i][j]['O2'],dat[i][j]['z'])
        print(lns[int(jctr)])
        ax[int(float(i)/3),int(float(i)%3)].semilogx(dat[i][j]['O2'],dat[i][j]['z'],\
                                                     linestyle=':',color=colors[int(jctr)],\
                                                     markersize=3,linewidth=lw,markevery=5)
        ax[int(float(i)/3),int(float(i)%3)].semilogx(dat[i][j]['H2'],dat[i][j]['z'],\
                                                     linestyle='-',color=colors[int(jctr)],\
                                                     markersize=3,linewidth=lw,markevery=5)
        ax[int(float(i)/3),int(float(i)%3)].semilogx(dat[i][j]['CO'],dat[i][j]['z'],\
                                                     linestyle='--',color=colors[int(jctr)],\
                                                     markersize=3,linewidth=lw,markevery=5)
        ax[int(float(i)/3),int(float(i)%3)].set_xlim([1e-8,1])
        
        jctr +=1
plt.savefig('mixtest.png') 


tlns = ['o','s','^','D','*']
tsizes = [10,9,10,8,12]
jctr = 0

temp = []
for i in list(dat.keys()):
    temp += [ax[int(float(i)/3),int(float(i)%3)].twiny()]
    jctr = 0
    for j in list(dat[i].keys()):
        print(list(dat[i].keys()))
        if j == '1e9':
            jctr += 1
            continue
        temp[-1].plot(dat[i][j]['t'][0],550,'o',color=colors[jctr],markersize=10)
        jctr +=1 
    temp[-1].set_xlim([190,280])
plt.subplots_adjust(hspace=0.25,wspace=0.25)
plt.savefig('ttest.png',dpi=300)
